package com.xuanzheng.company.servlet;

import com.google.gson.Gson;
import com.xuanzheng.company.entity.User;
import com.xuanzheng.company.service.CompanyEmployeeService;
import com.xuanzheng.company.service.impl.CompanyEmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/reviewResignation")
public class ReviewResignationServlet extends HttpServlet {
    private CompanyEmployeeService companyEmployeeService = new CompanyEmployeeServiceImpl();
    private Gson gson = new Gson();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Map<String, Object> result = new HashMap<>();
        
        // 获取当前登录用户
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute("user") == null) {
            result.put("success", false);
            result.put("message", "请先登录");
            out.print(gson.toJson(result));
            return;
        }
        
        User currentUser = (User) session.getAttribute("user");
        
        // 检查是否是公司负责人
        if (currentUser.getRole() != 1) {
            result.put("success", false);
            result.put("message", "只有公司负责人可以审核离职申请");
            out.print(gson.toJson(result));
            return;
        }
        
        String companyId = currentUser.getCompanyId();
        if (companyId == null || companyId.isEmpty()) {
            result.put("success", false);
            result.put("message", "您尚未加入公司");
            out.print(gson.toJson(result));
            return;
        }
        
        // 获取申请ID和审核结果
        String id = request.getParameter("id");
        String actionStr = request.getParameter("action");
        
        if (id == null || id.trim().isEmpty()) {
            result.put("success", false);
            result.put("message", "申请ID不能为空");
            out.print(gson.toJson(result));
            return;
        }
        
        if (actionStr == null || actionStr.trim().isEmpty()) {
            result.put("success", false);
            result.put("message", "请指定审核操作");
            out.print(gson.toJson(result));
            return;
        }
        
        int status;
        switch (actionStr) {
            case "approve": // 批准离职
                status = 4;
                break;
            case "reject": // 拒绝离职
                status = 1;
                break;
            default:
                result.put("success", false);
                result.put("message", "无效的操作：" + actionStr);
                out.print(gson.toJson(result));
                return;
        }
        
        // 处理审核
        boolean success = companyEmployeeService.reviewResignation(id, status);
        
        if (success) {
            result.put("success", true);
            result.put("message", status == 4 ? "已批准离职申请" : "已拒绝离职申请");
        } else {
            result.put("success", false);
            result.put("message", "处理离职申请失败，请稍后再试");
        }
        
        out.print(gson.toJson(result));
    }
} 